package com.atguigu.ucenterservice.controller;


import com.atguigu.common.R;
import com.atguigu.common.utils.JwtUtils;
import com.atguigu.common.vo.CommonMember;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.register.RegisterVo;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-20
 */
@RestController
@RequestMapping("/ucenterservice/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    @ApiOperation(value = "登录")
    public R loginUser(@RequestBody UcenterMember member) {
        //调用service方法实现登录查询，返回token字符串
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    @ApiOperation(value = "注册")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token字符串获取用户信息
    @GetMapping("getUserInfoToken")

    @ApiOperation(value = "根据token字符串获取用户信息")
    public R getUserInfoToken(HttpServletRequest request) {
        //根据token获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据用户id查询用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("member",member);
    }

    //根据用户id获取用户信息，为了创建订单使用
    @GetMapping("getCommonMember/{id}")
    @ApiOperation(value = "根据id获取用户信息")
    public CommonMember getCommonMember(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        CommonMember commonMember = new CommonMember();
        BeanUtils.copyProperties(member,commonMember);
        return commonMember;
    }

    //查询某一天注册人数
    @GetMapping("getCountRegister/{day}")
    @ApiOperation(value = "查询某天注册人数")
    public R getCountRegister(@PathVariable String day) {
        Integer count = memberService.countRegister(day);
        return R.ok().data("countRegister",count);
    }
}

